<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Penandatangan extends CI_Controller {
	
	function __construct(){		
		parent::__construct();	
		//$this->load->library('Excel_generator');
		//$this->output->enable_profiler(true);
		
		$this->load->model('mdl_penandatangan');
	}
	
	function index(){
		$data['page_title']='Penandatangan';
		$data['page_icon']='icon-pencil';
		$data['page_content']='penandatangan';
		
		$this->load->view('layout/master',$data);
	}
	
	
	function grid(){
		$data = $this->mdl_penandatangan->getdata();
		echo $this->mdl_penandatangan->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		
		$data['id_ttd'] = '';
		$data['jabatan_ttd'] = '';
		$data['nama_ttd'] = '';
		$data['nip_ttd'] = '';
		
		$this->load->view('penandatangan_form', $data);
	}

	function edit($id_ttd){
		$r = $this->mdl_penandatangan->getDataEdit($id_ttd);

		$data['id_ttd'] = $r->row()->id_ttd;
		$data['jabatan_ttd'] = $r->row()->jabatan_ttd;
		$data['nama_ttd'] = $r->row()->nama_ttd;
		$data['nip_ttd'] = $r->row()->nip_ttd;
		$data['status']=$r->row()->status;
		
		$this->load->view('penandatangan_form', $data);
	}
	
	function save($aksi="add"){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("id_ttd", 'Kode', 'trim|required|xss_clean');
		$this->form_validation->set_rules("jabatan_ttd", 'jabatan', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama_ttd", 'Nama', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nip_ttd", 'Nip', 'trim|required|xss_clean');
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if ($aksi=='add') {
				# code...
				$result = $this->mdl_penandatangan->InsertOnDb($data);
			}else{
				$result = $this->mdl_penandatangan->UpdateOnDb($data);
			}
			
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	}

	function delete($id_ttd){
		$result = $this->mdl_penandatangan->DeleteOnDb($id_ttd);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
}